package com.lill.org.api.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.R;
import com.lill.org.model.CompanyInfoModel;
import com.lill.org.service.ICompanyInfoService;
import com.lill.org.service.IEmployeeService;

@RestController
@RequestMapping("api/org/companyInfo")
public class CompanyInfoAction {

	@Autowired
	private ICompanyInfoService companyInfoService;
	@Autowired
	private IEmployeeService employeeService;

	@RequestMapping("/apply")
	public R save(@RequestBody CompanyInfoModel orgCompanyInfo) {
		if(StringUtils.isNotEmpty(orgCompanyInfo.getPhone())&&StringUtils.isNotEmpty(orgCompanyInfo.getAttn())){
    		Map<String, Object> mapQuery=new HashMap<String, Object>();
    		mapQuery.put("phone", orgCompanyInfo.getPhone());
    		List<Map<String, Object>> list = employeeService.queryPersonList(mapQuery);
    		if(null!=list&&!list.isEmpty()){
    			if(orgCompanyInfo.getPhone().equals(list.get(0).get("phone").toString())
    					&&orgCompanyInfo.getAttn().equals(list.get(0).get("name").toString())){
    				orgCompanyInfo.setPersonId(list.get(0).get("id").toString());
    			}else{
    				return R.error(1, "手机号已经被使用！");
    			}
    		}
    	}
		
		Map<String, Object> nameQuery=new HashMap<String, Object>();
    	nameQuery.put("nameeq", orgCompanyInfo.getName());
    	int total = companyInfoService.queryTotal(nameQuery);
    	if(total>0){
    		return R.error(1, "公司简称已经被使用！");
    	}

		orgCompanyInfo.setStatus(2);
		orgCompanyInfo.setCreateTime(new Date());
		int result = companyInfoService.save(orgCompanyInfo);
		if (result > 0) {
			return R.ok();
		} else {
			return R.failure();
		}
	}
}
